<?php
/**
 * 请求入参的基础参数封装
 **/

namespace App\Request;

use App\Exception\BusinessException;
use Hyperf\Validation\Request\FormRequest;

abstract class BaseRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [];
    }

    public function messages(): array
    {
        return [
            'required'    => ':attribute 字段不存在或不能为空|-10002',
            'regex'       => ':attribute 非法格式|-10003',
            'min'         => ':attribute 内容太短|-10004',
            'max'         => ':attribute 内容太长|-10005',
            'exists'      => ':attribute 非法数据|-10006',
            'in'          => ':attribute 无效数据|-10007',
            'integer'     => ':attribute 必须是整形|-10008',
            'date_format' => ':attribute 时间格式错误|-10009',
            'date'        => ':attribute 时间格式错误|-10010',
            'numeric'     => ':attribute 必须是数值|-10011',
            'required_if' => ':other 等于 :value 时 :attribute 字段不存在或不能为空|-10012',
            'url'         => ':attribute 必须为url|-10013',
            'json'        => ':attribute 必须为json类型|-10014',
        ];
    }

    /**
     * 处理 path rules数组
     * @param array $pathRulerArr
     * @return array
     */
    protected function handlePathRules(array $pathRulerArr): array
    {
        if (!key_exists($this->path(), $pathRulerArr)) {
            throw new BusinessException(-10005, '未能匹配rules内path');
        }
        return $pathRulerArr[$this->path()];
    }
}
